<?php

namespace Contacts\Model;

use Zend\Db\TableGateway\TableGateway;

class ContactViewTable
{
	protected $tableGateway;

	public function __construct(TableGateway $tableGateway)
	{
		$this->tableGateway = $tableGateway;
	}

	public function fetchAll()
	{
		$resultSet = $this->tableGateway->select();
		return $resultSet;
	}

	public function getContactView($id)
	{
		$id  = (int) $id;
		$rowset = $this->tableGateway->select(array('id' => $id));
		$row = $rowset->current();
		if (!$row) {
			throw new \Exception("Could not find row $id");
		}
		return $row;
	}

	public function saveContactView(ContactView $contactview)
	{
		$data = array(
				'id' => $contactview->id,
				
				'fullname'  => $contactview->fullname,
				'jobtitle'  => $contactview->jobtitle,
				
				'mobilephone'  => $contactview->mobilephone,
				
		);

		$id = (int) $contactview->id;
		if ($id == 0) {
			$this->tableGateway->insert($data);
		} else {
			if ($this->getContactView($id)) {
				$this->tableGateway->update($data, array('id' => $id));
			} else {
				throw new \Exception('ContactView id does not exist');
			}
		}
	}


	public function deleteContactView($id)
	{
		$this->tableGateway->delete(array('id' =>  $id));
	}
}